<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        style="width: 160px"
        class="filter-item"
        placeholder="请输入发票抬头"
        v-model="listQuery.title"
      ></el-input>
      <el-input
        style="width: 160px"
        class="filter-item"
        placeholder="请输入税号"
        v-model="listQuery.tax_no"
      ></el-input>

      <el-button class="filter-item" size="mini" plain @click="handleReset">重置</el-button>
      <el-button class="filter-item" size="mini" type="primary" @click="handleFilter"
        >查询</el-button
      >
    </div>
    <Table :data="list" :columns="columns" :loading="listLoading">
      <template v-slot:related_orders>
        <el-table-column label="关联订单号" width="200" showOverflowTooltip>
          <template slot-scope="scope">
            <span v-for="(item, index) in scope.row.related_orders" :key="item"
              >{{ item }} {{ index === scope.row.related_orders.length - 1 ? '' : ',' }}
            </span>
          </template>
        </el-table-column>
      </template>
      <template v-slot:status>
        <el-table-column label="开票状态" width="100" align="center">
          <template slot-scope="scope">
            <span
              :style="
                scope.row.status === 'invoiced'
                  ? 'color: #2AA23E'
                  : scope.row.status === 'not_invoiced'
                  ? 'color: #DC4A4A'
                  : ''
              "
              >{{ $invoiceStatus[scope.row.status] }}
            </span>
          </template>
        </el-table-column>
      </template>
      <template v-slot:action>
        <el-table-column label="操作" minWidth="160" fixed="right">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top">
              <i class="el-icon-edit table-action" @click="$refs.edit.handleEdit(scope.row)"></i>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="详情" placement="top">
              <i
                class="el-icon-tickets table-action"
                @click="$refs.detail.handleDetail(scope.row.id)"
              ></i>
            </el-tooltip>
          </template>
        </el-table-column>
      </template>
    </Table>
    <div class="pagination-container">
      <pagination
        :total="count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.size"
        @pagination="fetchData"
      />
    </div>
    <Edit ref="edit" />
    <Detail ref="detail" />
  </div>
</template>

<script>
import { getList } from '@/api/invoices'

import Edit from './components/Edit'
import Detail from './components/Detail'
export default {
  name: 'InvoiceIndex',
  components: { Edit, Detail },
  data() {
    return {
      list: null,
      count: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        title: '',
        tax_no: ''
      },
      columns: [
        {
          type: 'index',
          align: 'center',
          label: '序号',
          width: '50'
        },
        {
          label: '税号',
          width: '140',
          prop: 'tax_no',
          align: 'center'
        },
        {
          label: '企业名称',
          width: '160',
          prop: 'title',
          showOverflowTooltip: true
        },
        {
          label: '关联订单号',
          slot: 'related_orders'
        },

        {
          label: '开户行帐号',
          width: '160',
          prop: 'bank_account',
          showOverflowTooltip: true
        },
        {
          label: '开户行地址',
          width: '160',
          prop: 'bank_address',
          showOverflowTooltip: true
        },
        {
          label: '电子邮箱',
          width: '140',
          prop: 'email'
        },
        {
          label: '开票金额',
          width: '110',
          prop: 'amount',
          align: 'center'
        },
        {
          label: '开票创建时间',
          width: '160',
          prop: 'add_time',
          align: 'center'
        },
        { label: '开票状态', slot: 'status' },
        {
          label: '操作',
          slot: 'action'
        }
      ]
    }
  },

  created() {
    this.fetchData()
  },
  mounted() {},
  methods: {
    // 获取数据
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then((res) => {
        this.list = res.data.list_data
        this.count = res.data.paging.count
        this.listLoading = false
      })
    },

    // 筛选
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    /**
     * @description:
     * @return {*}
     */
    handleReset() {
      this.listQuery = {
        page: 1,
        size: 10,
        title: '',
        tax_no: ''
      }
      this.fetchData()
    }
  }
}
</script>
<style scoped>
.filter-container {
  justify-content: flex-start !important;
  flex-wrap: wrap;
}
.el-button {
  margin-bottom: 5px;
  margin-left: 0;
  margin-right: 5px;
}
</style>
